#coding=utf-8
from framework.db.mysql import BaseDb
from tornado import gen
from lib import log

class Boox(BaseDb):
    @gen.coroutine
    def list(self):
        sql=''' SELECT a.*,b.Fname,b.Fmin_price FROM tb_box_log AS a LEFT JOIN tb_box as b on a.Fbox_id=b.Fid LIMIT 0, 50 '''
        sql_cnt='SELECT count(Fid) as count FROM tb_box_log'
        try:
            ret = yield self.executejson(sql)
            
            count=yield self.executejson(sql_cnt)
            resp = {'ret': ret,'count':count[0]}
            print resp
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)

    @gen.coroutine
    def listsr(self,Fuser_phone,Fstatus):

            sql=''' SELECT a.*,b.Fname,b.Fmin_price FROM tb_box_log AS a LEFT JOIN tb_box as b
             on a.Fbox_id=b.Fid WHERE a.Fuser_phone like %s  AND a.Fstatus=%s LIMIT 0, 50 '''
            try:
                ret = yield self.executejson(sql,('%%%s%%' % Fuser_phone,Fstatus))
                
                print ret
            except Exception, e:
                log.log_exception(self.__class__.__name__, e)
                ret = '-1'
            raise gen.Return(ret)


    @gen.coroutine
    def status(self,Fid,Fstatus):
        
        sql='UPDATE tb_box_log SET Fstatus=%s WHERE Fid=%s'
        try:
            res=yield self.upsert(sql,(Fstatus,Fid))
 
        except Exception,e:
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(res)